﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using PanasonicWarranty.Business;
using System.Web.UI.HtmlControls;
using Utils;
namespace PanasonicWarranty.UI
{
    public partial class CustomerGroupManagement : BasePageManageWarranty
    {

        void paging_PagingChange(object sender, EventArgs e)
        {
            BindGridData();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindGridData();
            }
        }


        private void BindGridData()
        {
            try
            {
                grvData.DataSource = pa_CustomerGroup.GetAll();
                grvData.DataBind();
            }
            catch { }
        }

        protected void grvData_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                Authorize(Role.Write);
                int id = Convert.ToInt32(e.CommandArgument);
                hdfID.Value = id.ToString();
                switch (e.CommandName)
                {
                    case "Editing":
                        var obj = DB.pa_CustomerGroups.FirstOrDefault(p => p.CustomerGroupID == id);
                        if (obj != null)
                        {
                            txtCode.Text = obj.CustomerGroupCode;
                            txtName.Text = obj.CustomerGroupName;
                            txtDescription.Text = obj.Description ?? "";
                            txtOrder.Text = obj.OrderID == null ? "0" : obj.OrderID.ToString();
                            RegisterStartUpScript("OpenDialog('dialog1',400);");
                        }
                        break;
                    case "Deleting":
                        obj = DB.pa_CustomerGroups.FirstOrDefault(p => p.CustomerGroupID == id);
                        if (obj != null)
                        {
                            DB.pa_CustomerGroups.DeleteOnSubmit(obj);
                            DB.SubmitChanges();
                            //Ghi log dữ liệu
                            InsertChangeLog("Xóa dữ liệu nhóm khách hàng");
                            BindGridData();
                        }
                        break;
                }
            }
            catch (Exception ex)
            {
                HandleException(ex);
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                Authorize(Role.Write);
                if (ValidateInfo())
                {
                    pa_CustomerGroup obj;
                    if (hdfID.Value == "")//Add new
                    {
                        obj = new pa_CustomerGroup();
                    }
                    else //Edit
                    {
                        obj = DB.pa_CustomerGroups.FirstOrDefault(p => p.CustomerGroupID == Convert.ToInt32(hdfID.Value));
                    }
                    obj.CustomerGroupCode = txtCode.Text;
                    obj.CustomerGroupName = txtName.Text;
                    obj.Description = txtDescription.Text;
                    obj.OrderID = string.IsNullOrWhiteSpace(txtOrder.Text) ? 0 : Convert.ToInt32(txtOrder.Text.Trim());
                    //Save
                    if (hdfID.Value == "")
                        DB.pa_CustomerGroups.InsertOnSubmit(obj);
                    DB.SubmitChanges();
                    //Ghi log dữ liệu
                    InsertChangeLog(hdfID.Value == "" ? "Thêm mới dữ liệu nhóm khách hàng" : "Cập nhật dữ liệu nhóm khách hàng");
                    BindGridData();
                }
            }
            catch (Exception ex)
            {
                HandleException(ex);
            }
        }

        protected void lbtnAddNew_Click(object sender, EventArgs e)
        {
            try
            {
                Authorize(Role.Write);
                ControlClass.ResetControlValues(pnlDialogBlock);
                RegisterStartUpScript("OpenDialog('dialog1',400);");
            }
            catch (Exception ex)
            {
                HandleException(ex);
            }
        }

        protected override bool ValidateInfo()
        {
            string errMsg = string.Empty;
            if (string.IsNullOrWhiteSpace(txtName.Text))
                errMsg = "Bắt buộc nhập tên nhóm";
            if (string.IsNullOrWhiteSpace(txtCode.Text))
                errMsg = "Bắt buộc nhập mã nhóm";
            if (errMsg != string.Empty)
            {
                lbError.Text = errMsg;
                RegisterStartUpScript("OpenDialog('dialog1',400);");
                return false;
            }
            else
            {
                lbError.Text = string.Empty;
                return true;
            }
        }
    }
}